<?php
/******************************************************************************************
 * The contents of this file are subject to the SOURCE CODE AGREEMENT ("License Agreement")
 * which can be viewed at http://www.letrium.com/jetmobile-agreement
 *
 * BY DOWNLOADING AND/OR INSTALLING AND/OR USING THIS FILE YOU AGREE TO BE BOUND BY THE TERMS
 * OF THIS LICENSE AGREEMENT. IF YOU DO NOT AGREE TO THE TERMS OF THIS LICENSE AGREEMENT,
 * PLEASE DO NOT DOWNLOAD, INSTALL, RUN, OR OTHERWISE USE THE SOURCES OF THE SOFTWARE.
 * Under the terms of the license, You shall not, among other things:
 * 1) permit, authorize, license or sublicense any third party to view or use
 * the Source Code; 2) sell, rent, lease, distribute, make available, publish or
 * otherwise transfer the Source Code; 3) develop Forked Software;
 * 4) use the Source Code for anything other than its intended, legitimate,
 * and legal purpose.
 *
 * You do not have the right to remove Letrium copyrights from the source code
 * or user interface.
 *
 * To the maximum extent permitted by applicable law, Letrium shall not be liable
 * to Licensee for any incidental, consequential, special, punitive or
 * indirect damages, including without limitation, damages for loss of profits,
 * business opportunity, data or use, incurred by Licensee or any third party,
 * even if it has been advised of the possibility of such damages.
 * Letrium makes no representations or warranties with respect to the Source Code.
 * All express or implied representations and warranties, including without
 * limitation any implied warranty of merchantability, of fitness for a particular
 * purpose, of reliability or availability, of accuracy or completeness of responses,
 * of results, of workmanlike effort, of lack of viruses, and of lack of negligence,
 * is hereby expressly disclaimed. Licensee specifically acknowledges that the Source Code
 * is provided "as is" and may have bugs, errors, defects or deficiencies.
 *
 * Copyright (C) 2012 Letrium ltd.; All Rights Reserved.
 ******************************************************************************************/

require_once "custom/jetMobile/JetMobileEditView.php";

class JetMobileSearch extends JetMobileEditView
{
	public $formName = 'JetMobileSearch'; 
	
	public $view = 'Search';
	
	public function setup($module, $focus = null, $metadataFile = null, $tpl = 'custom/jetMobile/tpls/Search.tpl', $createFocus = true)
	{
		if ($metadataFile == null) {
			$file = "modules/{$module}/metadata/mobile.searchdefs.php";
			if (file_exists("custom/" . $file)) {
				$metadataFile  = "custom/" . $file;
			} else {
				$metadataFile = $file;
			}		
		}		
        $this->th = $this->getTemplateHandler();
        $this->th->ss =& $this->ss;
        $this->tpl = $tpl;
        $this->module = $module;
        $this->focus = $focus;
        if ($createFocus) {
            $this->createFocus();
        }
        $this->metadataFile = $metadataFile;
        include($this->metadataFile);
        $this->defs = $viewdefs[$this->module]['layout'][strtolower($this->view) . 'mobile'];
	}
	
	public function process($checkFormName = false, $formName = 'JetMobileSearch')
	{ 
		$this->fieldDefs = $this->focus->field_defs;
		
		$options = array();
        foreach ($this->focus->field_defs as $field => $def) {        
        	$options[$field] = isset($def['options']) ? $def['options'] : false;
        }		
        if (!$this->th->checkTemplate($this->module, $this->view, $checkFormName, $formName)) {
            $this->render();
        }
	foreach ($this->fieldDefs as $field => $def) {
			$this->fieldDefs[$field]['optionsKey'] = $options[$field];
		}
		
		$this->fieldDefs['current_user_only'] = array(
			'name' => 'current_user_only',
			'type' => 'bool',
		);
	}
	
	public function render()
	{
            $this->searchFields = $this->defs;
            foreach ($this->searchFields as $field => $def) {
                if ($def['name'] == 'current_user_only') {
                        $this->searchFields[$field]['customLabel'] = "<%= helper.string('Only My') %>";
                }
            }
	}
	
	public function display($showTitle = false, $ajaxSave = false)
        { 
            $this->th->ss->assign('APP', $GLOBALS['app_strings']);
            $this->th->ss->assign('MOD', $GLOBALS['mod_strings']);
            $this->th->ss->assign('fields', $this->fieldDefs);
            $this->th->ss->assign('config', $GLOBALS['sugar_config']);
            $this->th->ss->assign('module', $this->module);
            $this->th->ss->assign('current_user', $GLOBALS['current_user']);
            $this->th->ss->assign('bean', $this->focus);
            $this->th->ss->assign('searchFields', $this->searchFields);
            $this->th->ss->assign('view', $this->view);
            $this->th->ss->load_filter('output', 'trimwhitespace');
            $str = $this->th->displayTemplate($this->module, 'JetMobileSearch', $this->tpl, $ajaxSave, $this->defs);
            return $str;
        }
}

